Examples

Interactive

googleway

library(googleway)
set_key(read.dcf("~/Documents/.googleAPI", fields = "GOOGLE_MAP_KEY"))

The googleway library provides access to Google Maps API, both for creating maps, and using the mapping services such as geocoding and searching for places.

To use any of Google Map’s services you will need an API key

A choropleth is made by plotting coloured polygons on the map. In this example the polygons represent Statistical Areas (level 2) of melbourne, coloured according to the area of the region.

df <- melbourne
df$info <- paste0("SA2<br><b>",df$SA2_NAME,"</b>")

google_map() %>%
  add_polygons(
    data = df, fill_colour = "AREASQKM", stroke_colour = "AREASQKM", info_window = "info",
    palette = viridisLite::plasma
  )

other examples - polylines, circles, markers

google_map() %>%
  add_markers(data = tram_stops)

traffic

google_map() %>%
  add_traffic()

Given it uses Google Maps you can also use their streetview serivce

Mapdeck

library(mapdeck)
set_token(read.dcf("~/Documents/.googleAPI", fields = "MAPBOX"))

The mapdeck library provides access to Uber’s Deck.gl framework, and uses Mapbox GL as the mapping layer. Deck.gl’s use of WebGL enables it to render large datasets (up to millions of points) quickly. It also provides a ‘2.5d’ perspective of data, where the user can tilt, drag and rotate the map (as well as pan and zoom), and view data ‘extruded’ to give a perspective of height. To use Mapbox (through mapdeck) you will need an access token

This choropleth is showing the same data as in the googelway choropleth example, but a random ‘elevation’ value is added to show the data extruded from the map. While elevation provides a nice visual effect, care has to be taken to ensure it is meaningful. … continue with height vs area vs mass vs data value…

df <- melbourne
df$elevation <- sample(100:5000, size = nrow(df))
df$info <- paste0("SA2<br><b>",df$SA2_NAME,"</b>")

mapdeck(style = mapdeck_style('dark') , location = c(145, -37.8) 
        ,pitch = 45, zoom = 10) %>%
  add_polygon(
    data = df, polyline = "geometry", layer = "polygon_layer", fill_colour = "AREASQKM",
    stroke_colour = "AREASQKM",elevation = "elevation", stroke_width = 0, tooltip = 'info',
    palette = viridisLite::plasma )

lines

mapdeck(
  style = 'mapbox://styles/mapbox/dark-v9'
  , location = c(145, -37.8)
  , zoom = 10) %>%
  add_path(
    data = roads
    , stroke_colour = "RIGHT_LOC"
    , layer_id = "path_layer"
    , tooltip = "ROAD_NAME"
    , auto_highlight = TRUE
  )

Shiny

All the interactive maps listed are built using the htmlwidgets framework. Which is natively supported inside Shiny applications. This means you can include the maps in an interactive dashboard.